﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Hosting;
using System.Xml;
using visinia.SmartEngine.Cache;

namespace visinia.SmartEngine
{
    public class SystemMenu
    {
        public static string CallPage(string header, string page) 
        {
           return WebRoots.GetAdminRoot + "Default.aspx?header=" + header + "&page=" + page;
        }

        public static string CallPage(string header, string page, string parameterName, string parameterValue)
        {
            return WebRoots.GetAdminRoot + "Default.aspx?header=" + header + "&page=" + page + "&" + parameterName + "=" + parameterValue;
        }

        public static List<SystemMenuHeader> GetSystemMenu()
        {
            List<SystemMenuHeader> SystemMenuPages = (List<SystemMenuHeader>)DataCache.GetCache("SystemMenuPages");
            if (SystemMenuPages == null)
            {                              
                string pathPages = HostingEnvironment.MapPath(WebRoots.GetAdminRoot + "Pages");
                string[] pageDirs = Directory.GetFileSystemEntries(pathPages, "_*");
                SystemMenuPages = DataBind(pathPages, pageDirs, true);                
                DataCache.SetCache("SystemMenuPages", SystemMenuPages, new TimeSpan(0, 20, 0));
            }
            return SystemMenuPages;
        }

        public static List<SystemMenuHeader> GetPluginMenu()
        {
            List<SystemMenuHeader> PluginMenuPages = (List<SystemMenuHeader>)DataCache.GetCache("PluginMenuPages");
            if (PluginMenuPages == null)
            {                      
                string pathPages = HostingEnvironment.MapPath(WebRoots.GetAdminRoot + "Pages");
                string[] pluginPageDirs = Directory.GetFileSystemEntries(pathPages);
                PluginMenuPages = DataBind(pathPages, pluginPageDirs, false);
                DataCache.SetCache("PluginMenuPages", PluginMenuPages, new TimeSpan(0, 20, 0));
            }
            return PluginMenuPages;
        }

        public static void RefreshMenus()
        {
            DataCache.DeleteCache("SystemMenuPages");
            DataCache.DeleteCache("PluginMenuPages");
        }

        #region Data Binding
        private static List<SystemMenuHeader> DataBind(string path, string[] pageDirs, bool isSystemFolder)
        {
            List<SystemMenuHeader> SystemMenuPages = new List<SystemMenuHeader>();
            foreach (string dir in pageDirs)
            {
                string name = dir.Substring((dir.LastIndexOf('\\') + 1));
                //skip system directories for plugins
                if (!isSystemFolder)
                {
                    if (name.StartsWith("_"))
                        continue;
                }
                XmlDocument pagesdoc = new XmlDocument();
                string file = path + "\\" + name + "\\Menu.Data";
                if (File.Exists(file))
                {
                    pagesdoc.Load(file);
                    XmlNode pagesroot = pagesdoc.DocumentElement;
                    if (pagesroot == null)
                        continue;
                    SystemMenuHeader systemMenuPage = new SystemMenuHeader();
                    systemMenuPage.Header = pagesroot.Attributes["name"].Value;
                    systemMenuPage.Page = new List<SystemMenuPage>();
                    foreach (XmlNode child in pagesroot.ChildNodes)
                    {
                        SystemMenuPage menuData = new SystemMenuPage();
                        menuData.Name = child["name"].InnerText;
                        menuData.FriendlyName = child["friendlyname"].InnerText;
                        menuData.Control = child["control"].InnerText;
                        menuData.Visible = child["visible"].InnerText;
                        menuData.Role = child["role"].InnerText;
                        if (File.Exists(path + "\\" + child["help"].InnerText))
                        {
                            using (StreamReader reader = new StreamReader(path + "\\" + child["help"].InnerText))
                                menuData.Help = reader.ReadToEnd();
                        }
                        systemMenuPage.Page.Add(menuData);
                    }
                    SystemMenuPages.Add(systemMenuPage);
                }
            }
            return SystemMenuPages;
        }
        #endregion
    }
}
